package com.htc.android.footprints.view;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.vo.Footprint;

public class PlayButton extends ImageView
  implements View.OnClickListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener
{
  public static final int IDLE_STATE = 0;
  public static final int INTERNAL_ERROR = 2;
  public static final int NO_ERROR = 0;
  public static final int PAUSE_STATE = 2;
  public static final int PLAYING_STATE = 1;
  public static final int SDCARD_ACCESS_ERROR = 1;
  private static final String TAG = "PlayButton";
  private Footprint mFootprint;
  OnStateChangedListener mOnStateChangedListener = null;
  private MediaPlayer mPlayer = null;
  File mSampleFile = null;
  public int mSampleLength = 0;
  long mSampleStart = 0L;
  int mState = 0;

  public PlayButton(Context paramContext)
  {
    super(paramContext);
    initView();
    initFunction();
  }

  public PlayButton(Context paramContext, int paramInt1, int paramInt2)
  {
    super(paramContext);
    initView();
    initFunction();
  }

  public PlayButton(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet, 0);
    initView();
    initFunction();
  }

  public PlayButton(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
  {
    super(paramContext, paramAttributeSet, paramInt);
    initView();
    initFunction();
  }

  private void controlKeepScreenOn(int paramInt)
  {
    switch (paramInt)
    {
    default:
    case 0:
    case 1:
    case 2:
    }
    while (true)
    {
      return;
      ((Activity)getContext()).getWindow().clearFlags(128);
      continue;
      ((Activity)getContext()).getWindow().addFlags(128);
    }
  }

  private void initFunction()
  {
  }

  private void initView()
  {
    setBackgroundResource(2130837562);
    setScaleType(ImageView.ScaleType.CENTER);
    setIcon(this.mState);
    setOnClickListener(this);
  }

  private void setError(int paramInt)
  {
    if (this.mOnStateChangedListener != null)
      this.mOnStateChangedListener.onError(paramInt);
  }

  private void setIcon(int paramInt)
  {
    switch (paramInt)
    {
    default:
    case 0:
    case 2:
    case 1:
    }
    while (true)
    {
      return;
      setImageResource(2130837718);
      continue;
      setImageResource(2130837718);
      continue;
      setImageResource(2130837715);
    }
  }

  private void setState(int paramInt)
  {
    if (paramInt == this.mState);
    while (true)
    {
      return;
      this.mState = paramInt;
      setIcon(this.mState);
      signalStateChanged(this.mState);
      controlKeepScreenOn(this.mState);
    }
  }

  private void showToast(String paramString)
  {
    if (paramString != null)
      Toast.makeText(getContext(), paramString, 2000).show();
  }

  private void signalStateChanged(int paramInt)
  {
    if (this.mOnStateChangedListener != null)
      this.mOnStateChangedListener.onStateChanged(paramInt);
  }

  private void startPlayback()
  {
    stopPlayback();
    this.mPlayer = new MediaPlayer();
    if (this.mSampleFile == null);
    while (true)
    {
      return;
      try
      {
        this.mPlayer.setDataSource(this.mSampleFile.getAbsolutePath());
        this.mPlayer.setOnCompletionListener(this);
        this.mPlayer.setOnErrorListener(this);
        this.mPlayer.prepare();
        this.mPlayer.start();
        this.mSampleStart = System.currentTimeMillis();
        setState(1);
      }
      catch (IllegalArgumentException localIllegalArgumentException)
      {
        setError(2);
        this.mPlayer = null;
      }
      catch (IOException localIOException)
      {
        setError(1);
        this.mPlayer = null;
      }
    }
  }

  private void togglePlayback()
  {
    if (this.mPlayer == null);
    while (true)
    {
      return;
      if (this.mState != 0)
      {
        try
        {
          if (!this.mPlayer.isPlaying())
            break label48;
          this.mPlayer.pause();
          setState(2);
        }
        catch (IllegalStateException localIllegalStateException)
        {
          localIllegalStateException.printStackTrace();
        }
        continue;
        label48: this.mPlayer.start();
        setState(1);
        continue;
      }
    }
  }

  public void onClick(View paramView)
  {
    if ((this.mSampleFile == null) || (!this.mSampleFile.exists()))
      showToast(getResources().getString(2131361908));
    if (this.mState == 0)
      startPlayback();
    while (true)
    {
      return;
      if (this.mState == 2)
      {
        togglePlayback();
        continue;
      }
      if (this.mState != 1)
        continue;
      togglePlayback();
    }
  }

  public void onCompletion(MediaPlayer paramMediaPlayer)
  {
    stopPlayback();
    setImageResource(2130837718);
  }

  public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1, int paramInt2)
  {
    setError(paramInt1);
    setImageResource(2130837718);
    return true;
  }

  public void setOnStateChangedListener(OnStateChangedListener paramOnStateChangedListener)
  {
    this.mOnStateChangedListener = paramOnStateChangedListener;
  }

  public void setfootprintData(Footprint paramFootprint)
  {
    if (paramFootprint != null)
    {
      this.mFootprint = paramFootprint;
      if (FootprintUtils.checkAudioData(this.mFootprint))
      {
        this.mSampleFile = new File(this.mFootprint.mAudioPath);
        if (!this.mSampleFile.exists())
          this.mSampleFile = null;
      }
    }
  }

  public void stopPlayback()
  {
    if (this.mPlayer == null);
    while (true)
    {
      return;
      this.mPlayer.stop();
      this.mPlayer.release();
      this.mPlayer = null;
      setState(0);
    }
  }

  public static abstract interface OnCheckedChangeListener
  {
    public abstract void onCheckedChanged(PlayButton paramPlayButton, boolean paramBoolean);
  }

  public static abstract interface OnStateChangedListener
  {
    public abstract void onError(int paramInt);

    public abstract void onStateChanged(int paramInt);
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.view.PlayButton
 * JD-Core Version:    0.6.0
 */